﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data;
using MySql.Data.MySqlClient;

namespace YJ.Data.MySql
{
    public class ProgramBuilderValidates : YJ.Data.Interface.IProgramBuilderValidates
    {
        private DBHelper dbHelper = new DBHelper();
        /// <summary>
        /// 构造函数
        /// </summary>
        public ProgramBuilderValidates()
        {
        }
        /// <summary>
        /// 添加记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderValidates实体类</param>
        /// <returns>操作所影响的行数</returns>
        public int Add(YJ.Data.Model.ProgramBuilderValidates model)
        {
            string sql = @"INSERT INTO programbuildervalidates
				(ID,ProgramID,TableName,FieldName,FieldNote,Validate) 
				VALUES(@ID,@ProgramID,@TableName,@FieldName,@FieldNote,@Validate)";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID },
				new MySqlParameter("@ProgramID", MySqlDbType.VarChar, 36){ Value = model.ProgramID },
				new MySqlParameter("@TableName", MySqlDbType.Text, -1){ Value = model.TableName },
				new MySqlParameter("@FieldName", MySqlDbType.Text, -1){ Value = model.FieldName },
				model.FieldNote == null ? new MySqlParameter("@FieldNote", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@FieldNote", MySqlDbType.Text, -1) { Value = model.FieldNote },
				new MySqlParameter("@Validate", MySqlDbType.Int32, 11){ Value = model.Validate }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 更新记录
        /// </summary>
        /// <param name="model">YJ.Data.Model.ProgramBuilderValidates实体类</param>
        public int Update(YJ.Data.Model.ProgramBuilderValidates model)
        {
            string sql = @"UPDATE programbuildervalidates SET 
				ProgramID=@ProgramID,TableName=@TableName,FieldName=@FieldName,FieldNote=@FieldNote,Validate=@Validate
				WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ProgramID", MySqlDbType.VarChar, 36){ Value = model.ProgramID },
				new MySqlParameter("@TableName", MySqlDbType.Text, -1){ Value = model.TableName },
				new MySqlParameter("@FieldName", MySqlDbType.Text, -1){ Value = model.FieldName },
				model.FieldNote == null ? new MySqlParameter("@FieldNote", MySqlDbType.Text, -1) { Value = DBNull.Value } : new MySqlParameter("@FieldNote", MySqlDbType.Text, -1) { Value = model.FieldNote },
				new MySqlParameter("@Validate", MySqlDbType.Int32, 11){ Value = model.Validate },
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = model.ID }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 删除记录
        /// </summary>
        public int Delete(Guid id)
        {
            string sql = "DELETE FROM programbuildervalidates WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
        /// <summary>
        /// 将DataRedar转换为List
        /// </summary>
        private List<YJ.Data.Model.ProgramBuilderValidates> DataReaderToList(MySqlDataReader dataReader)
        {
            List<YJ.Data.Model.ProgramBuilderValidates> List = new List<YJ.Data.Model.ProgramBuilderValidates>();
            YJ.Data.Model.ProgramBuilderValidates model = null;
            while (dataReader.Read())
            {
                model = new YJ.Data.Model.ProgramBuilderValidates();
                model.ID = dataReader.GetString(0).ToGuid();
                model.ProgramID = dataReader.GetString(1).ToGuid();
                model.TableName = dataReader.GetString(2);
                model.FieldName = dataReader.GetString(3);
                if (!dataReader.IsDBNull(4))
                    model.FieldNote = dataReader.GetString(4);
                model.Validate = dataReader.GetInt32(5);
                List.Add(model);
            }
            return List;
        }
        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderValidates> GetAll()
        {
            string sql = "SELECT * FROM programbuildervalidates";
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql);
            List<YJ.Data.Model.ProgramBuilderValidates> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }
        /// <summary>
        /// 查询记录数
        /// </summary>
        public long GetCount()
        {
            string sql = "SELECT COUNT(*) FROM programbuildervalidates";
            long count;
            return long.TryParse(dbHelper.GetFieldValue(sql), out count) ? count : 0;
        }
        /// <summary>
        /// 根据主键查询一条记录
        /// </summary>
        public YJ.Data.Model.ProgramBuilderValidates Get(Guid id)
        {
            string sql = "SELECT * FROM programbuildervalidates WHERE ID=@ID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ID", MySqlDbType.VarChar, 36){ Value = id.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderValidates> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List.Count > 0 ? List[0] : null;
        }


        /// <summary>
        /// 查询所有记录
        /// </summary>
        public List<YJ.Data.Model.ProgramBuilderValidates> GetAll(Guid programID)
        {
            string sql = "SELECT * FROM ProgramBuilderValidates WHERE ProgramID=@ProgramID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ProgramID", MySqlDbType.VarChar){ Value = programID.ToString() }
			};
            MySqlDataReader dataReader = dbHelper.GetDataReader(sql, parameters);
            List<YJ.Data.Model.ProgramBuilderValidates> List = DataReaderToList(dataReader);
            dataReader.Close();
            return List;
        }

        /// <summary>
        /// 删除记录
        /// </summary>
        public int DeleteByProgramID(Guid id)
        {
            string sql = "DELETE FROM ProgramBuilderValidates WHERE ProgramID=@ProgramID";
            MySqlParameter[] parameters = new MySqlParameter[]{
				new MySqlParameter("@ProgramID", MySqlDbType.VarChar){ Value = id.ToString() }
			};
            return dbHelper.Execute(sql, parameters);
        }
    }
}